﻿#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdio.h>
#include <windows.h>
//VS2022 WIN11环境测试
int main()
{
	FILE* pf = fopen("test.txt", "w");
	fputs("abcdef", pf);//先将代码放在输出缓冲区
	printf("睡眠10秒-已经写数据了，打开test.txt⽂件，发现⽂件没有内容\n");
	Sleep(10000);
	printf("刷新缓冲区\n");
	fflush(pf);//刷新缓冲区时，才将输出缓冲区的数据写到⽂件（磁盘）
	//注：fflush 在⾼版本的VS上不能使⽤了
	printf("再睡眠10秒-此时，再次打开test.txt⽂件，⽂件有内容了\n");
	Sleep(10000);
	fclose(pf);
	//注：fclose在关闭⽂件的时候，也会刷新缓冲区
	pf = NULL;
	return 0;
}
#if 0
int main()
{
FILE * pr =	fopen("text.txt", "w");
if (pr == NULL)
{
	perror("fopen");
	return -1;
}

int ch = 0;
while ((ch = fgetc(pr)) != EOF)
{
	printf("%c\n", ch);
}
if (feof(pr))
{	
	printf("文件遇到文件末尾正常读取结束\n");
}
else if (ferror(pr))
{
	printf("非正常读取结束\n");
}
fclose(pr);
pr = NULL;
	return 0;
}

int main()
{
	
	FILE * pr = fopen("text.txt", "w");
	if (pr ==NULL)
	{
		perror("pr");
		return -1;
	}
	char arr1[99] = "fewfsdfgs";
	char arr2[99] = "abcdef";
	fputs(arr1, pr);
	fseek(pr, 0, SEEK_SET);
	fputs(arr2, pr);
	rewind(pr);
	int ret = ftell(pr);
	printf("%d\n", ret);
	fclose(pr);
	pr = NULL;

}

struct s
{
	char id[99];
	int age;
	float weight;
};
int main()
{
	char arr1[990] = { 0 };
	struct s s1 = { "646465465",18,56.8f};
	sprintf(arr1, "%s %d %f", s1.id, s1.age, s1.weight);
	printf("第一次打印：%s\n", arr1);
	struct s s2 = { 0 };
	sscanf(arr1, "%s %d %f", s2.id, &(s2.age), &(s2.weight));
	printf("第二次打印:%s %d %f", s2.id, s2.age, s2.weight);
	return 0;
}

struct s
{
	int age;
	float weight;
	char id[99];
};
int main()
{
	char arr1[990] = { 0 };
	struct s s1 = { 18,56.8,{"5732957492385"}};
	sprintf(arr1, "%d %.1f %s", s1.age, s1.weight, s1.id);
	printf("第一次打印：%s\n", arr1);
	struct s s2 = {0};
	sscanf(arr1, "%d %.1f %s", &(s2.age),&(s2.weight),s2.id);
	printf("第二次打印:%d %1.f %s",s2.age,s2.weight,s2.id);
	return 0;
}
int main()
{
	FILE* pr1 = fopen("text_copy.txt", "rb");
	if (pr1 == NULL)
	{
		perror("pr1");
		return -1;
	}
	int arr[5] = { 0 };
	int ret = fread(arr, sizeof(int), 5, pr1);
	printf("%d\n", ret);
	for (int i = 0; i < 5; i++)
	{
		printf("%d ", arr[i]);
	}
	fclose(pr1);
	pr1 = NULL;
	return 0;
}
int main()
{
	FILE* pr1 = fopen("text_copy.txt", "wb");
	if (pr1 == NULL)
	{
		perror("pr1");
		return -1;
	}
	char arr[5] = "ddddd";
	int ret = fwrite(arr, sizeof(char), 5, pr1);
	printf("%d\n", ret);
	for (int i = 0; i < 5; i++)
	{
		printf("%c ", arr[i]);
	}
	fclose(pr1);
	pr1 = NULL;
	return 0;
}
int main2()
{
	FILE* pr1 = fopen("text_copy.txt", "w");
	if (pr1 == NULL)
	{
		perror("pr1");
		return -1;
	}
	//这里在打开文件时已经清空了
	//所以执行seek之后文件指针还是起始位置
	fseek(pr1, 0, SEEK_END);
	int ret = fprintf(pr1, "%d %c", 7, 'o');
	printf("%d\n", ret);
	fclose(pr1);
	pr1 = NULL;
	return 0;
}
int main()
{
	FILE* pr1 = fopen("text_copy.txt", "rb");
	if (pr1 == NULL)
	{
		perror("pr1");
		return -1;
	}
	char arr[5] = {0};
	int ret = fread(arr,sizeof(char),5,pr1);
	printf("%d\n", ret);
	for (int i = 0; i < 5; i++)
	{
		printf("%c ", arr[i]);
	}
	fclose(pr1);
	pr1 = NULL;
	return 0;
}
int main()
{
	FILE* pr1 = fopen("text_copy.txt", "w");
	if (pr1 == NULL)
	{
		perror("pr1");
		return -1;
	}
	fseek(pr1, 0, SEEK_END);
	int ret = fprintf(pr1, "%d %c", 7, 'o');
	printf("%d\n", ret);
	fclose(pr1);
	pr1 = NULL;
	return 0;
}

int main()
{
	FILE* pr1 = fopen("text.txt", "r");
	if (pr1 == NULL)
	{
		perror("pr1");
		return -1;
	}
	int a, b;
	char arr1[99] = { 0 };
	char arr2[99] = { 0 };
	int ret = fscanf(stdin,"%c%c",arr1,arr2);
	printf("%d\n",ret);
	/*printf("%d\n",a);*/
	
	/*printf("%s", arr2);*/
	return 0;
}

int  main()
{

	//原文件
	FILE* pr1 = fopen("text.txt", "r");
	if (pr1 == NULL)
	{
		perror("pr1");
		return -1;
	}
	FILE* pr2 = fopen("text_copy.txt", "w");
	if (pr2 == NULL)
	{
		perror("pr2");
		return -1;
	}
	char arr1[99] = { 0 };
	//边读边写
	while ((fgets(arr1,6,pr1)) != NULL)
	{
		fputs(arr1,pr2);
		printf("%s", arr1);
	}
	fclose(pr2);
	pr2 = NULL;
	fclose(pr1);
	pr1 = NULL;
	return 0;
}

int main()
{
	FILE* pr = fopen("text.txt", "r");
	if (NULL == pr)
	{
		perror("pr");
		return -1;
	}
	char arr1[99] = { 0 };
	char arr2[99] = { 0 };
	fgets(arr1, 3, stdin);//它只能读2个字符如果有其他多的放在缓冲区
	printf("%s\n", arr1);
	fgets(arr2, 3, stdin);
	printf("%s", arr2);
	fclose(pr);
	pr = NULL;
	return 0;
}

int main()
{

	//原文件
	FILE* pr1 = fopen("text.txt", "r");
	if (pr1 == NULL)
	{
		perror("pr1");
		return -1;
	}
	FILE* pr2 = fopen("text_copy.txt", "w");
	if (pr2 == NULL)
	{
		perror("pr2");
		return -1;
	}
	//边读边写
	int ch = 0;
	while ((ch = fgetc(pr1)) != EOF)
	{
		fputc(ch, pr2);
		printf("%c", ch);
	}
	fclose(pr2);
	pr2 = NULL;
	fclose(pr1);
	pr1 = NULL;
	return 0;
}

int main()
{
	FILE* pr = fopen("text.txt", "w");
	if (pr == NULL)
	{
		perror("text.txt");
		return 1;
	}
	fputc('c', pr);
	fclose(pr);
	pr = NULL;
	return 0;
}

int main()
{
	FILE* pf = fopen("text.txt", "wb");
	if (pf == NULL)
	{
		perror("text.txt");
		return 1;
	}
	int arr = 8;
	fwrite(&arr, sizeof(int), 1, pf);
	fclose(pf);
	pf = NULL;
	return 0;
}

int main()
{
	FILE* pf = fopen("text.txt", "wb");
	if (pf == NULL)
	{
		perror("text.txt");
		return 1;
	}

	int arr[7] = { 1,2,3,4,5,6,7 };
	int* tem = arr;
	for (int i = 0; i < 7; i++)
	{
		fwrite(tem, sizeof(int), 1, pf);
		tem++;
	}
	fclose(pf);
	pf = NULL;
	return 0;
}
#endif


